package com.yuntsg.nnsfcp.service.back.Role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuntsg.nnsfcp.entity.RolePermissionOperationReqVO;
import com.yuntsg.nnsfcp.front.entity.TRolePermission;
import com.yuntsg.nnsfcp.front.mapper.TRolePermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author i2twmx@gmail.com
 * @date 2020/5/28 3:35 下午
 * @description :
 */
@Service
@Slf4j
public class RolePermissionService {

    @Resource
    TRolePermissionMapper tRolePermissionMapper;

    /**
     * 根据roleid 获取 权限列表
     *
     * @param roleIds
     * @return
     */
    public List<Integer> getPerByRoleId(List<Integer> roleIds) {
        List<TRolePermission> rolePermissions = Optional.ofNullable(tRolePermissionMapper.selectList(new LambdaQueryWrapper<TRolePermission>().in(TRolePermission::getRoleId, roleIds))).orElse(Collections.singletonList(new TRolePermission()));
        if (rolePermissions.size() > 0) {
            return rolePermissions.stream().map(TRolePermission::getPerId).collect(Collectors.toList());
        }
        return null;
    }


    public int removeByRoleId(Integer roleId) {
        return tRolePermissionMapper.delete(new LambdaQueryWrapper<TRolePermission>().eq(TRolePermission::getRoleId, roleId));
    }

    //
//
//    public List<Long> getPermissionIdsByRoles(List<Long> roleIds) {
//        List<RolePermission> list = this.list(new LambdaQueryWrapper<RolePermission>().in(RolePermission::getPerId, roleIds));
//        return list.stream().map(RolePermission::getPerId).distinct().collect(Collectors.toList());
//    }
    public void addRolePermission(RolePermissionOperationReqVO vo) {
        List<TRolePermission> list = new ArrayList<>();
        for (Integer permissionId : vo.getPermissionIds()) {
            TRolePermission rolePermission = new TRolePermission();
            rolePermission.setPerId(permissionId);
            rolePermission.setRoleId(vo.getRoleId());
            rolePermission.setCreated(LocalDateTime.now());
            list.add(rolePermission);
        }
        tRolePermissionMapper.delete(new LambdaQueryWrapper<TRolePermission>().eq(TRolePermission::getRoleId, vo.getRoleId()));
        for (TRolePermission tRolePermission : list) {
            tRolePermissionMapper.insert(tRolePermission);
        }
    }


    //    public boolean removeByPermissionId(Long permissionId) {
//        return this.remove(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPerId,permissionId));
//    }
//
//
//    public List<Long> getRoleIds(Long permissionId) {
//        List<RolePermission> list = this.list(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPerId, permissionId));
//        return list.stream().map(RolePermission::getRoleId).collect(Collectors.toList());
//    }
//
//
    public List<Integer> getPermissionIdsByRoleId(Integer roleId) {
        List<TRolePermission> list = tRolePermissionMapper.selectList(new LambdaQueryWrapper<TRolePermission>().eq(TRolePermission::getRoleId, roleId));
        return list.stream().map(TRolePermission::getPerId).collect(Collectors.toList());
    }
}
